﻿/*
 *Copyright(C) 2020 by  GYYX All rights reserved.
 *Unity版本：2018.4.23f1 
 *作者:程一峰  
 *创建日期: 2021-04-08 
 *模块说明：
 *版本: 1.2
*/

using UnityEditor;
using UnityEngine;
using Warfare.FSM;

namespace WarfareEditor.FSM
{
    /// <summary>
    /// 文件处理器；
    /// </summary>
    public class FSMZoom_FileHandle : FSMZoom
    {

        protected internal override void OnDestroy() { }

        private FSMAsset CurrentAsset
        {
            get { return window.CurrentAsset; }
            set
            {
                if (window.CurrentAsset == value)
                    return;
                window.CurrentAsset = value;
            }
        }

        protected internal override void OnGUI()
        {
            GUILayout.Space(5);
            EditorGUILayout.BeginVertical("box");

            CurrentAsset = EditorGUILayout.ObjectField(CurrentAsset, typeof(FSMAsset), false) as FSMAsset;

            GUILayout.Space(5);

            if (CurrentAsset != null)
            {
                CurrentAsset.Descripe = EditorGUILayout.TextField(CurrentAsset.Descripe);
                GUILayout.Space(5);
            }

            EditorGUILayout.BeginHorizontal();

            if (GUILayout.Button("新建"))
            {
                string path = EditorUtility.SaveFilePanel("选择配置文件位置", $" {Application.dataPath}/Game/Conf/AI", "NewFSMAsset", "asset");
                CreateFSM(path);
            }

            if (CurrentAsset != null)
            {
                if (GUILayout.Button("保存"))
                    SaveCurrentFMSAsset();
                if (GUILayout.Button("删除"))
                    DeleteCurrentFSM();
            }

            EditorGUILayout.EndHorizontal();
            GUILayout.Space(5);
            EditorGUILayout.EndVertical();
        }

        /// <summary>
        /// 制作一个新的FSM；
        /// </summary>
        /// <param name="path"></param>
        private void CreateFSM(string path)
        {
            if (string.IsNullOrEmpty(path))
                return;

            int index = path.IndexOf("Asset");
            path = path.Substring(index);
            FSMAsset newFsm = new FSMAsset();
            newFsm.CreateRootNode();
            //创建
            AssetDatabase.CreateAsset(newFsm, path);
            SaveCurrentFMSAsset();
            //获取；
            newFsm = AssetDatabase.LoadAssetAtPath<FSMAsset>(path);
            //ToDo : 这里需要自动设置 Addressable 包名。

            //放到目前的窗口；
            window.CurrentAsset = newFsm;
        }

        /// <summary>
        /// 删除当前状态机；
        /// </summary>
        private void DeleteCurrentFSM()
        {
            var curAsset = window.CurrentAsset;
            if (curAsset == null)
                return;


            string path = AssetDatabase.GetAssetPath(curAsset);
            AssetDatabase.DeleteAsset(path);
            SaveCurrentFMSAsset();
        }

        /// <summary>
        /// 编辑器保存；
        /// </summary>
        private void SaveCurrentFMSAsset()
        {
            var curAsset = window.CurrentAsset;
            if (curAsset == null)
                return;

            EditorUtility.SetDirty(curAsset);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }

    }
}